<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>motrackers.track &mdash; Multi-object trackers in Python 1.0.0 documentation</title>
      <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
      <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <!--[if lt IE 9]>
    <script src="../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
        <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/underscore.js"></script>
        <script src="../../_static/_sphinx_javascript_frameworks_compat.js"></script>
        <script src="../../_static/doctools.js"></script>
        <script src="../../_static/sphinx_highlight.js"></script>
    <script src="../../_static/js/theme.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
            <a href="../../index.html" class="icon icon-home"> Multi-object trackers in Python
          </a>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../includeme/readmefile.html">Multi-object trackers in Python</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../includeme/readmefile.html#example-tf-mobilenetssd-centroidtracker">Example: <cite>TF-MobileNetSSD + CentroidTracker</cite></a></li>
<li class="toctree-l1"><a class="reference internal" href="../../includeme/readmefile.html#example-yolov3-centroidtracker">Example: <cite>YOLOv3 + CentroidTracker</cite></a></li>
<li class="toctree-l1"><a class="reference internal" href="../../includeme/apidocuments.html">Tracker</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../includeme/apidocuments.html#sort">SORT</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../includeme/apidocuments.html#iou-tracker">IOU Tracker</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../includeme/apidocuments.html#kalman-filter-based-centroid-tracker">Kalman Filter based Centroid Tracker</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../includeme/apidocuments.html#tracks">Tracks</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../includeme/apidocuments.html#kalman-filters">Kalman Filters</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../includeme/apidocuments.html#object-detection">Object Detection</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../includeme/apidocuments.html#utilities">Utilities</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../includeme/apidocuments.html#references-and-credits">References and Credits</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../includeme/apidocuments.html#contributor-covenant-code-of-conduct">Contributor Covenant Code of Conduct</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">Multi-object trackers in Python</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../../index.html" class="icon icon-home"></a> &raquo;</li>
          <li><a href="../index.html">Module code</a> &raquo;</li>
      <li>motrackers.track</li>
      <li class="wy-breadcrumbs-aside">
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <h1>Source code for motrackers.track</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">from</span> <span class="nn">motrackers.kalman_tracker</span> <span class="kn">import</span> <span class="n">KFTracker2D</span><span class="p">,</span> <span class="n">KFTrackerSORT</span><span class="p">,</span> <span class="n">KFTracker4D</span>


<div class="viewcode-block" id="Track"><a class="viewcode-back" href="../../includeme/apidocuments.html#motrackers.track.Track">[docs]</a><span class="k">class</span> <span class="nc">Track</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Track containing attributes to track various objects.</span>

<span class="sd">    Args:</span>
<span class="sd">        frame_id (int): Camera frame id.</span>
<span class="sd">        track_id (int): Track Id</span>
<span class="sd">        bbox (numpy.ndarray): Bounding box pixel coordinates as (xmin, ymin, width, height) of the track.</span>
<span class="sd">        detection_confidence (float): Detection confidence of the object (probability).</span>
<span class="sd">        class_id (str or int): Class label id.</span>
<span class="sd">        lost (int): Number of times the object or track was not tracked by tracker in consecutive frames.</span>
<span class="sd">        iou_score (float): Intersection over union score.</span>
<span class="sd">        data_output_format (str): Output format for data in tracker.</span>
<span class="sd">            Options include ``[&#39;mot_challenge&#39;, &#39;visdrone_challenge&#39;]``. Default is ``mot_challenge``.</span>
<span class="sd">        kwargs (dict): Additional key word arguments.</span>

<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">count</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="n">metadata</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span>
        <span class="n">data_output_formats</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;mot_challenge&#39;</span><span class="p">,</span> <span class="s1">&#39;visdrone_challenge&#39;</span><span class="p">]</span>
    <span class="p">)</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span>
        <span class="bp">self</span><span class="p">,</span>
        <span class="n">track_id</span><span class="p">,</span>
        <span class="n">frame_id</span><span class="p">,</span>
        <span class="n">bbox</span><span class="p">,</span>
        <span class="n">detection_confidence</span><span class="p">,</span>
        <span class="n">class_id</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
        <span class="n">lost</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
        <span class="n">iou_score</span><span class="o">=</span><span class="mf">0.</span><span class="p">,</span>
        <span class="n">data_output_format</span><span class="o">=</span><span class="s1">&#39;mot_challenge&#39;</span><span class="p">,</span>
        <span class="o">**</span><span class="n">kwargs</span>
    <span class="p">):</span>
        <span class="k">assert</span> <span class="n">data_output_format</span> <span class="ow">in</span> <span class="n">Track</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s1">&#39;data_output_formats&#39;</span><span class="p">]</span>
        <span class="n">Track</span><span class="o">.</span><span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">id</span> <span class="o">=</span> <span class="n">track_id</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">detection_confidence_max</span> <span class="o">=</span> <span class="mf">0.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lost</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">age</span> <span class="o">=</span> <span class="mi">0</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">frame_id</span><span class="p">,</span> <span class="n">bbox</span><span class="p">,</span> <span class="n">detection_confidence</span><span class="p">,</span> <span class="n">class_id</span><span class="o">=</span><span class="n">class_id</span><span class="p">,</span> <span class="n">lost</span><span class="o">=</span><span class="n">lost</span><span class="p">,</span> <span class="n">iou_score</span><span class="o">=</span><span class="n">iou_score</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">data_output_format</span> <span class="o">==</span> <span class="s1">&#39;mot_challenge&#39;</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">output</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_mot_challenge_format</span>
        <span class="k">elif</span> <span class="n">data_output_format</span> <span class="o">==</span> <span class="s1">&#39;visdrone_challenge&#39;</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">output</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_vis_drone_format</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">NotImplementedError</span>

<div class="viewcode-block" id="Track.update"><a class="viewcode-back" href="../../includeme/apidocuments.html#motrackers.track.Track.update">[docs]</a>    <span class="k">def</span> <span class="nf">update</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">frame_id</span><span class="p">,</span> <span class="n">bbox</span><span class="p">,</span> <span class="n">detection_confidence</span><span class="p">,</span> <span class="n">class_id</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">lost</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">iou_score</span><span class="o">=</span><span class="mf">0.</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Update the track.</span>

<span class="sd">        Args:</span>
<span class="sd">            frame_id (int): Camera frame id.</span>
<span class="sd">            bbox (numpy.ndarray): Bounding box pixel coordinates as (xmin, ymin, width, height) of the track.</span>
<span class="sd">            detection_confidence (float): Detection confidence of the object (probability).</span>
<span class="sd">            class_id (int or str): Class label id.</span>
<span class="sd">            lost (int): Number of times the object or track was not tracked by tracker in consecutive frames.</span>
<span class="sd">            iou_score (float): Intersection over union score.</span>
<span class="sd">            kwargs (dict): Additional key word arguments.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">class_id</span> <span class="o">=</span> <span class="n">class_id</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">bbox</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">bbox</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">detection_confidence</span> <span class="o">=</span> <span class="n">detection_confidence</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">frame_id</span> <span class="o">=</span> <span class="n">frame_id</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">iou_score</span> <span class="o">=</span> <span class="n">iou_score</span>

        <span class="k">if</span> <span class="n">lost</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lost</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lost</span> <span class="o">+=</span> <span class="n">lost</span>

        <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">detection_confidence_max</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">detection_confidence_max</span><span class="p">,</span> <span class="n">detection_confidence</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">age</span> <span class="o">+=</span> <span class="mi">1</span></div>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">centroid</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return the centroid of the bounding box.</span>

<span class="sd">        Returns:</span>
<span class="sd">            numpy.ndarray: Centroid (x, y) of bounding box.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">bbox</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">+</span><span class="mf">0.5</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">bbox</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">bbox</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">+</span><span class="mf">0.5</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">bbox</span><span class="p">[</span><span class="mi">3</span><span class="p">]))</span>

<div class="viewcode-block" id="Track.get_mot_challenge_format"><a class="viewcode-back" href="../../includeme/apidocuments.html#motrackers.track.Track.get_mot_challenge_format">[docs]</a>    <span class="k">def</span> <span class="nf">get_mot_challenge_format</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Get the tracker data in MOT challenge format as a tuple of elements containing</span>
<span class="sd">        `(frame, id, bb_left, bb_top, bb_width, bb_height, conf, x, y, z)`</span>

<span class="sd">        References:</span>
<span class="sd">            - Website : https://motchallenge.net/</span>

<span class="sd">        Returns:</span>
<span class="sd">            tuple: Tuple of 10 elements representing `(frame, id, bb_left, bb_top, bb_width, bb_height, conf, x, y, z)`.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">mot_tuple</span> <span class="o">=</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">frame_id</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">id</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">bbox</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">bbox</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">bbox</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">bbox</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">detection_confidence</span><span class="p">,</span>
            <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span>
        <span class="p">)</span>
        <span class="k">return</span> <span class="n">mot_tuple</span></div>

<div class="viewcode-block" id="Track.get_vis_drone_format"><a class="viewcode-back" href="../../includeme/apidocuments.html#motrackers.track.Track.get_vis_drone_format">[docs]</a>    <span class="k">def</span> <span class="nf">get_vis_drone_format</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Track data output in VISDRONE Challenge format with tuple as</span>
<span class="sd">        `(frame_index, target_id, bbox_left, bbox_top, bbox_width, bbox_height, score, object_category,</span>
<span class="sd">        truncation, occlusion)`.</span>

<span class="sd">        References:</span>
<span class="sd">            - Website : http://aiskyeye.com/</span>
<span class="sd">            - Paper : https://arxiv.org/abs/2001.06303</span>
<span class="sd">            - GitHub : https://github.com/VisDrone/VisDrone2018-MOT-toolkit</span>
<span class="sd">            - GitHub : https://github.com/VisDrone/</span>

<span class="sd">        Returns:</span>
<span class="sd">            tuple: Tuple containing the elements as `(frame_index, target_id, bbox_left, bbox_top, bbox_width, bbox_height,</span>
<span class="sd">            score, object_category, truncation, occlusion)`.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">mot_tuple</span> <span class="o">=</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">frame_id</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">id</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">bbox</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">bbox</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">bbox</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">bbox</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">detection_confidence</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">class_id</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span>
        <span class="p">)</span>
        <span class="k">return</span> <span class="n">mot_tuple</span></div>

<div class="viewcode-block" id="Track.predict"><a class="viewcode-back" href="../../includeme/apidocuments.html#motrackers.track.Track.predict">[docs]</a>    <span class="k">def</span> <span class="nf">predict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Implement to prediction the next estimate of track.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="bp">NotImplemented</span></div>

    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">print_all_track_output_formats</span><span class="p">():</span>
        <span class="nb">print</span><span class="p">(</span><span class="n">Track</span><span class="o">.</span><span class="n">metadata</span><span class="p">[</span><span class="s1">&#39;data_output_formats&#39;</span><span class="p">])</span></div>


<div class="viewcode-block" id="KFTrackSORT"><a class="viewcode-back" href="../../includeme/apidocuments.html#motrackers.track.KFTrackSORT">[docs]</a><span class="k">class</span> <span class="nc">KFTrackSORT</span><span class="p">(</span><span class="n">Track</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Track based on Kalman filter tracker used for SORT MOT-Algorithm.</span>

<span class="sd">    Args:</span>
<span class="sd">        track_id (int): Track Id</span>
<span class="sd">        frame_id (int): Camera frame id.</span>
<span class="sd">        bbox (numpy.ndarray): Bounding box pixel coordinates as (xmin, ymin, width, height) of the track.</span>
<span class="sd">        detection_confidence (float): Detection confidence of the object (probability).</span>
<span class="sd">        class_id (str or int): Class label id.</span>
<span class="sd">        lost (int): Number of times the object or track was not tracked by tracker in consecutive frames.</span>
<span class="sd">        iou_score (float): Intersection over union score.</span>
<span class="sd">        data_output_format (str): Output format for data in tracker.</span>
<span class="sd">            Options ``[&#39;mot_challenge&#39;, &#39;visdrone_challenge&#39;]``. Default is ``mot_challenge``.</span>
<span class="sd">        process_noise_scale (float): Process noise covariance scale or covariance magnitude as scalar value.</span>
<span class="sd">        measurement_noise_scale (float): Measurement noise covariance scale or covariance magnitude as scalar value.</span>
<span class="sd">        kwargs (dict): Additional key word arguments.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">track_id</span><span class="p">,</span> <span class="n">frame_id</span><span class="p">,</span> <span class="n">bbox</span><span class="p">,</span> <span class="n">detection_confidence</span><span class="p">,</span> <span class="n">class_id</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">lost</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">iou_score</span><span class="o">=</span><span class="mf">0.</span><span class="p">,</span>
                 <span class="n">data_output_format</span><span class="o">=</span><span class="s1">&#39;mot_challenge&#39;</span><span class="p">,</span> <span class="n">process_noise_scale</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span> <span class="n">measurement_noise_scale</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">bbz</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">bbox</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">+</span><span class="mf">0.5</span><span class="o">*</span><span class="n">bbox</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">bbox</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">+</span><span class="mf">0.5</span><span class="o">*</span><span class="n">bbox</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span> <span class="n">bbox</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">*</span><span class="n">bbox</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span> <span class="n">bbox</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">/</span><span class="nb">float</span><span class="p">(</span><span class="n">bbox</span><span class="p">[</span><span class="mi">3</span><span class="p">])])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">kf</span> <span class="o">=</span> <span class="n">KFTrackerSORT</span><span class="p">(</span>
            <span class="n">bbz</span><span class="p">,</span> <span class="n">process_noise_scale</span><span class="o">=</span><span class="n">process_noise_scale</span><span class="p">,</span> <span class="n">measurement_noise_scale</span><span class="o">=</span><span class="n">measurement_noise_scale</span><span class="p">)</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">track_id</span><span class="p">,</span> <span class="n">frame_id</span><span class="p">,</span> <span class="n">bbox</span><span class="p">,</span> <span class="n">detection_confidence</span><span class="p">,</span> <span class="n">class_id</span><span class="o">=</span><span class="n">class_id</span><span class="p">,</span> <span class="n">lost</span><span class="o">=</span><span class="n">lost</span><span class="p">,</span>
                         <span class="n">iou_score</span><span class="o">=</span><span class="n">iou_score</span><span class="p">,</span> <span class="n">data_output_format</span><span class="o">=</span><span class="n">data_output_format</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

<div class="viewcode-block" id="KFTrackSORT.predict"><a class="viewcode-back" href="../../includeme/apidocuments.html#motrackers.track.KFTrackSORT.predict">[docs]</a>    <span class="k">def</span> <span class="nf">predict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Predicts the next estimate of the bounding box of the track.</span>

<span class="sd">        Returns:</span>
<span class="sd">            numpy.ndarray: Bounding box pixel coordinates as (xmin, ymin, width, height) of the track.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">kf</span><span class="o">.</span><span class="n">x</span><span class="p">[</span><span class="mi">6</span><span class="p">]</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">kf</span><span class="o">.</span><span class="n">x</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">kf</span><span class="o">.</span><span class="n">x</span><span class="p">[</span><span class="mi">6</span><span class="p">]</span> <span class="o">*=</span> <span class="mf">0.0</span>

        <span class="n">x</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">kf</span><span class="o">.</span><span class="n">predict</span><span class="p">()</span>

        <span class="k">if</span> <span class="n">x</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">*</span> <span class="n">x</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">])</span>

        <span class="n">w</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">*</span> <span class="n">x</span><span class="p">[</span><span class="mi">3</span><span class="p">])</span>
        <span class="n">h</span> <span class="o">=</span> <span class="n">x</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">/</span> <span class="nb">float</span><span class="p">(</span><span class="n">w</span><span class="p">)</span>
        <span class="n">bb</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">-</span><span class="mf">0.5</span><span class="o">*</span><span class="n">w</span><span class="p">,</span> <span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">-</span><span class="mf">0.5</span><span class="o">*</span><span class="n">h</span><span class="p">,</span> <span class="n">w</span><span class="p">,</span> <span class="n">h</span><span class="p">])</span>
        <span class="k">return</span> <span class="n">bb</span></div>

<div class="viewcode-block" id="KFTrackSORT.update"><a class="viewcode-back" href="../../includeme/apidocuments.html#motrackers.track.KFTrackSORT.update">[docs]</a>    <span class="k">def</span> <span class="nf">update</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">frame_id</span><span class="p">,</span> <span class="n">bbox</span><span class="p">,</span> <span class="n">detection_confidence</span><span class="p">,</span> <span class="n">class_id</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">lost</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">iou_score</span><span class="o">=</span><span class="mf">0.</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">update</span><span class="p">(</span>
            <span class="n">frame_id</span><span class="p">,</span> <span class="n">bbox</span><span class="p">,</span> <span class="n">detection_confidence</span><span class="p">,</span> <span class="n">class_id</span><span class="o">=</span><span class="n">class_id</span><span class="p">,</span> <span class="n">lost</span><span class="o">=</span><span class="n">lost</span><span class="p">,</span> <span class="n">iou_score</span><span class="o">=</span><span class="n">iou_score</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="n">z</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">bbox</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">+</span><span class="mf">0.5</span><span class="o">*</span><span class="n">bbox</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">bbox</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">+</span><span class="mf">0.5</span><span class="o">*</span><span class="n">bbox</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span> <span class="n">bbox</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">*</span><span class="n">bbox</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span> <span class="n">bbox</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">/</span><span class="nb">float</span><span class="p">(</span><span class="n">bbox</span><span class="p">[</span><span class="mi">3</span><span class="p">])])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">kf</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">z</span><span class="p">)</span></div></div>


<div class="viewcode-block" id="KFTrack4DSORT"><a class="viewcode-back" href="../../includeme/apidocuments.html#motrackers.track.KFTrack4DSORT">[docs]</a><span class="k">class</span> <span class="nc">KFTrack4DSORT</span><span class="p">(</span><span class="n">Track</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Track based on Kalman filter tracker used for SORT MOT-Algorithm.</span>

<span class="sd">    Args:</span>
<span class="sd">        track_id (int): Track Id</span>
<span class="sd">        frame_id (int): Camera frame id.</span>
<span class="sd">        bbox (numpy.ndarray): Bounding box pixel coordinates as (xmin, ymin, width, height) of the track.</span>
<span class="sd">        detection_confidence (float): Detection confidence of the object (probability).</span>
<span class="sd">        class_id (str or int): Class label id.</span>
<span class="sd">        lost (int): Number of times the object or track was not tracked by tracker in consecutive frames.</span>
<span class="sd">        iou_score (float): Intersection over union score.</span>
<span class="sd">        data_output_format (str): Output format for data in tracker.</span>
<span class="sd">            Options ``[&#39;mot_challenge&#39;, &#39;visdrone_challenge&#39;]``. Default is ``mot_challenge``.</span>
<span class="sd">        process_noise_scale (float): Process noise covariance scale or covariance magnitude as scalar value.</span>
<span class="sd">        measurement_noise_scale (float): Measurement noise covariance scale or covariance magnitude as scalar value.</span>
<span class="sd">        kwargs (dict): Additional key word arguments.</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">track_id</span><span class="p">,</span> <span class="n">frame_id</span><span class="p">,</span> <span class="n">bbox</span><span class="p">,</span> <span class="n">detection_confidence</span><span class="p">,</span> <span class="n">class_id</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">lost</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">iou_score</span><span class="o">=</span><span class="mf">0.</span><span class="p">,</span>
                 <span class="n">data_output_format</span><span class="o">=</span><span class="s1">&#39;mot_challenge&#39;</span><span class="p">,</span> <span class="n">process_noise_scale</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span> <span class="n">measurement_noise_scale</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span>
                 <span class="n">kf_time_step</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">kf</span> <span class="o">=</span> <span class="n">KFTracker4D</span><span class="p">(</span>
            <span class="n">bbox</span><span class="o">.</span><span class="n">copy</span><span class="p">(),</span> <span class="n">process_noise_scale</span><span class="o">=</span><span class="n">process_noise_scale</span><span class="p">,</span> <span class="n">measurement_noise_scale</span><span class="o">=</span><span class="n">measurement_noise_scale</span><span class="p">,</span>
            <span class="n">time_step</span><span class="o">=</span><span class="n">kf_time_step</span><span class="p">)</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">track_id</span><span class="p">,</span> <span class="n">frame_id</span><span class="p">,</span> <span class="n">bbox</span><span class="p">,</span> <span class="n">detection_confidence</span><span class="p">,</span> <span class="n">class_id</span><span class="o">=</span><span class="n">class_id</span><span class="p">,</span> <span class="n">lost</span><span class="o">=</span><span class="n">lost</span><span class="p">,</span>
                         <span class="n">iou_score</span><span class="o">=</span><span class="n">iou_score</span><span class="p">,</span> <span class="n">data_output_format</span><span class="o">=</span><span class="n">data_output_format</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

<div class="viewcode-block" id="KFTrack4DSORT.predict"><a class="viewcode-back" href="../../includeme/apidocuments.html#motrackers.track.KFTrack4DSORT.predict">[docs]</a>    <span class="k">def</span> <span class="nf">predict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">x</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">kf</span><span class="o">.</span><span class="n">predict</span><span class="p">()</span>
        <span class="n">bb</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">x</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span> <span class="n">x</span><span class="p">[</span><span class="mi">6</span><span class="p">],</span> <span class="n">x</span><span class="p">[</span><span class="mi">9</span><span class="p">]])</span>
        <span class="k">return</span> <span class="n">bb</span></div>

<div class="viewcode-block" id="KFTrack4DSORT.update"><a class="viewcode-back" href="../../includeme/apidocuments.html#motrackers.track.KFTrack4DSORT.update">[docs]</a>    <span class="k">def</span> <span class="nf">update</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">frame_id</span><span class="p">,</span> <span class="n">bbox</span><span class="p">,</span> <span class="n">detection_confidence</span><span class="p">,</span> <span class="n">class_id</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">lost</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">iou_score</span><span class="o">=</span><span class="mf">0.</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">update</span><span class="p">(</span>
            <span class="n">frame_id</span><span class="p">,</span> <span class="n">bbox</span><span class="p">,</span> <span class="n">detection_confidence</span><span class="p">,</span> <span class="n">class_id</span><span class="o">=</span><span class="n">class_id</span><span class="p">,</span> <span class="n">lost</span><span class="o">=</span><span class="n">lost</span><span class="p">,</span> <span class="n">iou_score</span><span class="o">=</span><span class="n">iou_score</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">kf</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">bbox</span><span class="o">.</span><span class="n">copy</span><span class="p">())</span></div></div>


<div class="viewcode-block" id="KFTrackCentroid"><a class="viewcode-back" href="../../includeme/apidocuments.html#motrackers.track.KFTrackCentroid">[docs]</a><span class="k">class</span> <span class="nc">KFTrackCentroid</span><span class="p">(</span><span class="n">Track</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Track based on Kalman filter used for Centroid Tracking of bounding box in MOT.</span>

<span class="sd">    Args:</span>
<span class="sd">        track_id (int): Track Id</span>
<span class="sd">        frame_id (int): Camera frame id.</span>
<span class="sd">        bbox (numpy.ndarray): Bounding box pixel coordinates as (xmin, ymin, width, height) of the track.</span>
<span class="sd">        detection_confidence (float): Detection confidence of the object (probability).</span>
<span class="sd">        class_id (str or int): Class label id.</span>
<span class="sd">        lost (int): Number of times the object or track was not tracked by tracker in consecutive frames.</span>
<span class="sd">        iou_score (float): Intersection over union score.</span>
<span class="sd">        data_output_format (str): Output format for data in tracker.</span>
<span class="sd">            Options ``[&#39;mot_challenge&#39;, &#39;visdrone_challenge&#39;]``. Default is ``mot_challenge``.</span>
<span class="sd">        process_noise_scale (float): Process noise covariance scale or covariance magnitude as scalar value.</span>
<span class="sd">        measurement_noise_scale (float): Measurement noise covariance scale or covariance magnitude as scalar value.</span>
<span class="sd">        kwargs (dict): Additional key word arguments.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">track_id</span><span class="p">,</span> <span class="n">frame_id</span><span class="p">,</span> <span class="n">bbox</span><span class="p">,</span> <span class="n">detection_confidence</span><span class="p">,</span> <span class="n">class_id</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">lost</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">iou_score</span><span class="o">=</span><span class="mf">0.</span><span class="p">,</span>
                 <span class="n">data_output_format</span><span class="o">=</span><span class="s1">&#39;mot_challenge&#39;</span><span class="p">,</span> <span class="n">process_noise_scale</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span> <span class="n">measurement_noise_scale</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">c</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">((</span><span class="n">bbox</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">+</span><span class="mf">0.5</span><span class="o">*</span><span class="n">bbox</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">bbox</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">+</span><span class="mf">0.5</span><span class="o">*</span><span class="n">bbox</span><span class="p">[</span><span class="mi">3</span><span class="p">]))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">kf</span> <span class="o">=</span> <span class="n">KFTracker2D</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="n">process_noise_scale</span><span class="o">=</span><span class="n">process_noise_scale</span><span class="p">,</span> <span class="n">measurement_noise_scale</span><span class="o">=</span><span class="n">measurement_noise_scale</span><span class="p">)</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">track_id</span><span class="p">,</span> <span class="n">frame_id</span><span class="p">,</span> <span class="n">bbox</span><span class="p">,</span> <span class="n">detection_confidence</span><span class="p">,</span> <span class="n">class_id</span><span class="o">=</span><span class="n">class_id</span><span class="p">,</span> <span class="n">lost</span><span class="o">=</span><span class="n">lost</span><span class="p">,</span>
                         <span class="n">iou_score</span><span class="o">=</span><span class="n">iou_score</span><span class="p">,</span> <span class="n">data_output_format</span><span class="o">=</span><span class="n">data_output_format</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

<div class="viewcode-block" id="KFTrackCentroid.predict"><a class="viewcode-back" href="../../includeme/apidocuments.html#motrackers.track.KFTrackCentroid.predict">[docs]</a>    <span class="k">def</span> <span class="nf">predict</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Predicts the next estimate of the bounding box of the track.</span>

<span class="sd">        Returns:</span>
<span class="sd">            numpy.ndarray: Bounding box pixel coordinates as (xmin, ymin, width, height) of the track.</span>

<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">s</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">kf</span><span class="o">.</span><span class="n">predict</span><span class="p">()</span>
        <span class="n">xmid</span><span class="p">,</span> <span class="n">ymid</span> <span class="o">=</span> <span class="n">s</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">s</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
        <span class="n">w</span><span class="p">,</span> <span class="n">h</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">bbox</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="bp">self</span><span class="o">.</span><span class="n">bbox</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
        <span class="n">xmin</span> <span class="o">=</span> <span class="n">xmid</span> <span class="o">-</span> <span class="mf">0.5</span><span class="o">*</span><span class="n">w</span>
        <span class="n">ymin</span> <span class="o">=</span> <span class="n">ymid</span> <span class="o">-</span> <span class="mf">0.5</span><span class="o">*</span><span class="n">h</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">xmin</span><span class="p">,</span> <span class="n">ymin</span><span class="p">,</span> <span class="n">w</span><span class="p">,</span> <span class="n">h</span><span class="p">])</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="nb">int</span><span class="p">)</span></div>

<div class="viewcode-block" id="KFTrackCentroid.update"><a class="viewcode-back" href="../../includeme/apidocuments.html#motrackers.track.KFTrackCentroid.update">[docs]</a>    <span class="k">def</span> <span class="nf">update</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">frame_id</span><span class="p">,</span> <span class="n">bbox</span><span class="p">,</span> <span class="n">detection_confidence</span><span class="p">,</span> <span class="n">class_id</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">lost</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">iou_score</span><span class="o">=</span><span class="mf">0.</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">update</span><span class="p">(</span>
            <span class="n">frame_id</span><span class="p">,</span> <span class="n">bbox</span><span class="p">,</span> <span class="n">detection_confidence</span><span class="p">,</span> <span class="n">class_id</span><span class="o">=</span><span class="n">class_id</span><span class="p">,</span> <span class="n">lost</span><span class="o">=</span><span class="n">lost</span><span class="p">,</span> <span class="n">iou_score</span><span class="o">=</span><span class="n">iou_score</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">kf</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">centroid</span><span class="p">)</span></div></div>
</pre></div>

           </div>
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2021, Aditya M. Deshpande.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>